package apiTestResultBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetPageCount;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/1/2.
 * 返回测试记录
 * type 和 caseId都可以为空
 */
public class QueryApiTestResult implements TestServerBusinessImpl {
    //每页总数
    final Integer everyPage = 5;
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        GetPageCount getPageCount = new GetPageCount(myConfig);
        ReturnJson returnJson = new ReturnJson();
        //读取请求参数
        String action = request.getParameter("action");
        String toPage = request.getParameter("toPage");

        if (Objects.equals("apiReportTest", action)){
            String module = request.getParameter("module");
            String path = request.getParameter("path");
            String restime = request.getParameter("restime");
            String caseid = request.getParameter("caseid");
            String userCase = request.getParameter("userCase");
            String exetime = request.getParameter("exetime");
            //greater less
            String gl = request.getParameter("gl");
            //这里是where模块组装
            String whereModule = "";
            ArrayList<String> whereModuleArr = new ArrayList<String>();
            if (!Objects.equals(module, null) && !Objects.equals(module, "")){
                whereModuleArr.add(" i.module=\"" + module + "\" ");
            }
            if (!Objects.equals(path, null) && !Objects.equals(path, "")){
                whereModuleArr.add(" i.path like \"%" + path + "%\" ");
            }
            if (!Objects.equals(caseid, null) && !Objects.equals(caseid, "")){
                whereModuleArr.add(" i.caseid=\"" + caseid + "\" ");
            }
            if (!Objects.equals(userCase, null) && !Objects.equals(userCase, "")){
                whereModuleArr.add(" i.userCase like \"%" + userCase + "%\" ");
            }
            if (!Objects.equals(exetime, null) && !Objects.equals(exetime, "")){
                whereModuleArr.add(" i.executeTime like \"" + exetime + "%\" ");
            }
            if (!Objects.equals(restime, null) && !Objects.equals(restime, "")){
                //因为responseTime比较特殊，对3做特殊处理，直接查询responseResult表
                //如果一定要查询其他的秒数，那就用like和or的方式组装sql语句
                if (Objects.equals(restime, "3")){
                    if (Objects.equals(gl, "greater")){
                        whereModuleArr.add(" i.responseResult = \"超时\" ");
                    }
                    if (Objects.equals(gl, "less")){
                        whereModuleArr.add(" i.responseResult = \"耗时正常\" ");
                    }
                }
            }

            //把where整体组装
            if (whereModuleArr.size() > 0){
                whereModule = " where ";
                for (int i = 0; i < whereModuleArr.size(); i++){
                    String oneWhereModule = whereModuleArr.get(i);
                    whereModule = whereModule + oneWhereModule;
                    if (i == whereModuleArr.size()-1){
                        break;
                    }
                    whereModule = whereModule + " AND ";
                }
            }

            //limit组装
            String limit = "";
            if (!Objects.equals(toPage, null) && !Objects.equals(toPage, "")){
                limit = getPageCount.getLimit(Integer.valueOf(toPage), everyPage);
            }

            // SELECT i.id, i.system, i.module, i.path, i.caseId, i.userCase, i.responseTime, i.executeTime, i.responseResult FROM `interfacetime` i
            //数据库查询
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String select = "SELECT i.id, i.system, i.module, i.path, i.caseId, i.userCase, i.responseTime, i.executeTime, i.responseResult ";
            String from = " FROM `interfacetime` i ";
            String where = whereModule ;
            String orderBy = " ORDER BY i.executeTime DESC ";
            String sqlWord = select + from + where + orderBy + limit + " ;";
            System.out.println(sqlWord);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sqlWord);

            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
                JSONObject resultObj = new JSONObject();
                JSONArray resultArr = new JSONArray();
                for (ArrayList<String> arrayLists : sqlReturnModel.getSelectReturnData()){
                    JSONArray oneResult = new JSONArray();
                    oneResult.add(arrayLists.get(0));
                    oneResult.add(arrayLists.get(1));
                    oneResult.add(arrayLists.get(2));
                    oneResult.add(arrayLists.get(3));
                    oneResult.add(arrayLists.get(4));
                    oneResult.add(arrayLists.get(5));
                    oneResult.add(arrayLists.get(6));
                    oneResult.add(arrayLists.get(7));
                    oneResult.add(arrayLists.get(8));
                    resultArr.add(oneResult);
                }
                resultObj.put("resultArr", resultArr);

                //获取页面总数
                select = "SELECT count(*) ";
                String countSql = select + from + where + ";";
                Integer count = getPageCount.getCount(countSql);
                Integer remained = count % everyPage > 0 ? 1 : 0;
                Integer pageCount = count/everyPage + remained;
                resultObj.put("pageCount", pageCount);

                returnJson.setResult(resultObj);
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
            }
        }

        if (Objects.equals("apiReport", action)){

        }

        if (Objects.equals("pyPageCount", action)){

        }

        if (Objects.equals("UIPageCount", action)){

        }



        return returnJson.toString();
    }
}
